﻿using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Win32;
using System.Windows.Forms;
using System.ComponentModel;

namespace AnaViewer
{
    public class AutoStartHelper
    {
        /// <summary>
        /// 语录阅览器的启动项名称。
        /// </summary>
        const string KeyName = "AnaViewer";
        /// <summary>
        /// 启动命令的参数
        /// </summary>
        static string CmdLine
        {
            get { return Application.ExecutablePath + " -AnaViewer"; }
        }

        public static void AutoStartSwitch(bool IStart)
        {
            AutoStartSwitch(KeyName, IStart);
        }
        static void AutoStartSwitch(string KeyNameValue, bool IStart)
        {
            if (IStart)
            {
                SetAutoValue(KeyNameValue);
            }
            else
            {
                DelAutoValue(KeyNameValue);
            }
        }
        public static bool IsValueExist()
        {
            return IsValueExist(KeyName);
        }
        static bool IsValueExist(string KeyValue)
        {
            RegistryKey localMachine = Registry.LocalMachine;
            RegistryKey key2 = localMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
            string str = null;
            try
            {
                str = (string)key2.GetValue(KeyValue);
                localMachine.Close();
            }
            catch (Exception)// exception)
            {
                //MessageBox.Show(exception.Message.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            if (str == null)
            {
                return false;
            }
            return true;
        }
        static void SetAutoValue(string KeyValue)
        {
            //即使存在，也覆盖它
            //if (!IsValueExist(KeyValue))
            {
                RegistryKey localMachine = Registry.LocalMachine;
                RegistryKey key2 = localMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                try
                {
                    key2.SetValue(KeyValue, CmdLine);
                    //MessageBox.Show(" 已经设置为自启动。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    localMachine.Close();
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
            }
        }
        static void DelAutoValue(string KeyValue)
        {
            if (IsValueExist(KeyValue))
            {
                RegistryKey localMachine = Registry.LocalMachine;
                RegistryKey key2 = localMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                try
                {
                    key2.DeleteValue(KeyValue);
                    //MessageBox.Show(" 自启动已经取消。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    localMachine.Close();
                }
                catch (Exception)// exception)
                {
                    //MessageBox.Show(exception.Message.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
            }
        }
    }
}
